package com.oasistudio.tk.connector.imp;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TreeMap;

import org.json.JSONArray;
import org.json.JSONObject;

import com.oasistudio.tk.connector.TopAPIConnectorHelper;
import com.oasistudio.tk.connector.TopApiRequest;
import com.oasistudio.tk.connector.TopApiResponse;
import com.taobao.tbw.common.TBWContext;

public class UsersGetConnector implements TopAPIConnectorHelper {
	private String nicks;
	private String fields="user_id,nick,seller_credit,type,consumer_protection";

	public UsersGetConnector() {
	}

	public void setNicks(String nicks){
		this.nicks = nicks;
	}

	public void setFields(String fields){
		this.fields = fields;
	}

	@Override
	public Object asyncPaser(byte[] part) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getApiUrl() {
		TopApiRequest request = new TopApiRequest();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		TreeMap<String, String> params = new TreeMap<String, String>();
		//???系�????
		params.put("method", "taobao.users.get");
		params.put("session", TBWContext.getInstance().getTopsession());
		params.put("timestamp", sdf.format(calendar.getTime()));
		params.put("format", "json");
		params.put("app_key", TBWContext.getInstance().getAppKey());
		params.put("v", "2.0");
		params.put("partner_id", "top-apitools");
		params.put("sign_method", "md5");
		//API method ???
		params.put("fields", fields);
		params.put("nicks", nicks);
		request.params = params;
		String url = request.generalRequestUrl(true);
		System.out.println("UsersGetConnector.getApiUrl() url = "+url);
		return url;
	}

	@Override
	public Object syncPaser(byte[] all) {
		UsersInfo usersInfo = new UsersInfo();
		try {
			TopApiResponse response = new TopApiResponse("users_get_response");
			String str = new String(all, "UTF-8");
			//Log.d("return msg:", str);
			if (response.parseResult(str).success) {
				usersInfo.isSuccess = true;
				JSONObject jsObj = response.data;
				if (jsObj.has("users")) {
					JSONObject itemsJSON = jsObj.getJSONObject("users");
					if (itemsJSON.has("user")) {
						JSONArray itemsArray = itemsJSON.getJSONArray("user");
						User[] users = new User[itemsArray.length()];
						usersInfo.users = users;
						for (int i = 0; i < itemsArray.length(); i++) {
							JSONObject user = itemsArray.getJSONObject(i);
							users[i] = new User();
							users[i].user_id = user.optLong("user_id");
							users[i].nick = user.optString("nick");
							users[i].type = user.optString("type");
							users[i].seller_credit = user.getJSONObject("seller_credit").optInt("level");
							users[i].consumer_protection = user.optBoolean("consumer_protection");
						}
					}
				}
			} else {
				usersInfo.isSuccess = false;
				usersInfo.errCode = response.errCode;
				usersInfo.errInfo = response.errInfo;
			}

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return usersInfo;
	}

	public static class UsersInfo {
		public String errCode;
		public String errInfo;
		public Boolean isSuccess;
		public User[] users;
	}

	public static class User {
		public Number user_id;//
		public String nick;//
		public Number seller_credit;//
		public String type;//
		public Boolean consumer_protection;//
		
		
		public String toString(){
			StringBuilder sb = new StringBuilder();
			sb.append("user_id:"+user_id);
			sb.append(";nick:"+nick);
			sb.append(";type:"+type);
			sb.append(";seller_credit:"+seller_credit);
			return sb.toString();
		}
	}
}
